import 'dart:async';

import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:get/get.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/data/user_info_entity.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/routes/app_pages.dart';
import 'package:mtf/app/utils/AppUtils.dart';
import 'package:mtf/app/utils/cache/AppCahche.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:url_launcher/url_launcher.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class mineController extends BaseController {
  RefreshController refreshController =
      RefreshController(initialRefresh: false);

  var user = AppCache.getUserInfo().obs;
  var content ="".obs;
  var day="".obs;
  var isShowTime=false.obs;

  @override
  void onInit() {
    super.onInit();
    startTimer();
  }

  void clickAuth() {
    AppUtils.loginCheck(() {
      var authStatus = user.value.authStatus;
      if(authStatus==1){
        //认证中
        Get.toNamed(Routes.SUCCESS,arguments: {"title":"认证提交成功","desc":"等待平台审核，请耐心等候","type":1,"buttonText":"返回"});
      }else if(authStatus==2){
        //已认证
        Get.toNamed(Routes.AUTHENTICATION_INFO);
      }else{
        //未认证
        Get.toNamed(Routes.AUTHENTICATION);
      }

    });
  }

  void clickSetting() {
    AppUtils.loginCheck(() {
      Get.toNamed(Routes.SETTING);
    });
  }

  void clickProperty() {
    AppUtils.authCheck(() {
      Get.toNamed(Routes.PROPERTY);
    });
  }

  void clickTransactionRecode(){
    AppUtils.authAndMt5Check(() {
      Get.toNamed(Routes.TRANSACTION_RECORD);
    });
  }

  void refreshData() {
    reqUserInfo();
    reqAgreement();
  }

  void reqUserInfo() async {
    var resp=await HttpManager.reqUserInfo();
    if(resp.isSuccess()&&resp.data!=null){
      AppCache.setUserInfo(resp.data!);
      user.value = resp.data!;
    }
    refreshController.refreshCompleted();
  }

  void reqAgreement() async{
    var resp=await HttpManager.reqAgreement();
    if(resp.isSuccessData()){
      content.value=resp.data?.authNotice.content ?? "";
      var time=resp.data?.authNotice.timeInt ?? 0;
      isShowTime.value=time>0;
      var dayTemp=(time.toDouble()/86400).ceil().toStringAsFixed(0);
      if(int.parse(dayTemp)<10){
        dayTemp="0${dayTemp}";
      }
      day.value=dayTemp;
    }
  }

  void clickActivity() async{
    AppUtils.loginCheck(() async {
      var step=user.value.getCurrentStep();
      if(step==0){
        //实名认证
        await Get.toNamed(Routes.AUTHENTICATION);
        refreshData();
      }else if(step==1){
        //添加银行卡
        await Get.toNamed(Routes.BANK_ADD);
        refreshData();
      }else if(step==2){
        //填写报告
        await Get.toNamed(Routes.QUESTIONNAIRE);
        refreshData();
      }else if(step==3){
        //充值
        await Get.toNamed(Routes.RECHARGE);
        refreshData();
      }
    });
  }

  void clickKeFu() async{
    var resp=await HttpManager.reqServer();
    if(resp.isSuccessData()){
      Get.toNamed(Routes.SERVER,arguments: {"url": resp.data?.kefuUrl ??""});
      // await launch(resp.data?.kefuUrl ??"");
    }

    // if(resp.isSuccessData()){
    //   AppUtils.loginCheck(() {
    //     showOkAlertDialog(
    //         context: Get.context!!,
    //         title: '联系方式',
    //         message: '${resp.data?.contact}',
    //         okLabel: "关闭",
    //         useRootNavigator: false,
    //         fullyCapitalizedForMaterial: false,
    //         barrierDismissible: false,
    //         style: AdaptiveStyle.iOS);
    //   });
    // }
  }

  late Timer timer;

  void startTimer() {
    print('startTimer');
    // Timer.periodic方法可以反复执行，这里设置执行周期为60s
    timer = Timer.periodic(const Duration(seconds: 45), (timer) {
      // var currentPage=Get.currentRoute;
      // if(currentPage==Routes.HOME){
      //
      // }
      if (AppCache.isLogin()) {
        print('mine_controller reqUserInfo');
        reqUserInfo();
      } else {
        timer.cancel();
      }
    });

  }


  @override
  void dispose() {
    super.dispose();
    if (timer != null) {
      timer.cancel();
    }
  }

}
